package com.lj.mcore.service;

import com.lj.mcore.dao.base.BaseAppDAO;
import com.lj.mcore.entity.Promo;
import com.lj.mcore.utils.CommUtils;
import com.lj.mcore.utils.PageBean;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
@Service
@Transactional
public class PromoService implements IPromoService{

    @Resource(name="promoDao")
    private BaseAppDAO<Promo> promoDao;

    @Override
    public boolean save(Promo obj) {
        return promoDao.save(obj);
    }

    @Override
    public boolean delete(Promo obj) {
        return promoDao.delete(obj);
    }

    @Override
    public boolean update(Promo obj) {
        return promoDao.update(obj);
    }

    @Override
    public Promo getObjById(Long id) {
        return promoDao.findById(id);
    }

    @Override
    public Promo getSingleResult(Map<String, Object> map) {
        return promoDao.getSingleResult(map);
    }

    @Override
    public List<Promo> query(String paramString, Map paramMap, int paramInt1, int paramInt2) {
        return promoDao.query(paramString, paramMap, paramInt1, paramInt2);
    }

    @Override
    public PageBean getList(int pageSize, int page, Map<String, String> query) {

        StringBuffer sb = new StringBuffer("from Promo obj where obj.deleteStatus = false ");
        if(query != null && query.size() >0 ){

            if(!CommUtils.null2String(query.get("gid")).equals("")){
                sb.append(" and obj.goods.id = "+query.get("gid"));
            }

            if(!CommUtils.null2String(query.get("gcid")).equals("")){
                sb.append(" and obj.goodsClass.id = "+query.get("gcid"));
            }

            if(!CommUtils.null2String(query.get("sid")).equals("")){
                sb.append(" and obj.store.id = "+query.get("sid"));
            }
        }
        sb.append(" order by obj.addTime desc");

        String hql = sb.toString();

        List listNum = this.query("select count(obj.id) "+hql,null,0,1);
        int allRow=0;
        if (listNum!=null){
            allRow = CommUtils.null2Int(listNum.get(0));
        }
        int totalPage = PageBean.countTotalPage(pageSize, allRow);
        final int offset = PageBean.countOffset(pageSize, page);
        final int length = pageSize;
        final int currentPage = PageBean.countCurrentPage(page);
        List list = this.query(hql,null, offset, length);
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(list);
        pageBean.init();
        return pageBean;
    }
}
